﻿#pragma once

#include "TaskThread.h"

namespace AsyncLib
{

class CUiTimerTask;

class CMainTaskThread : public CTaskThread
{
public:
	CMainTaskThread();
	~CMainTaskThread() override;

	void CreateWnd();
	void Stop() override;
	void Exit() override;
	void PostTask(std::function<void()>&& fnTask) override;
	std::shared_ptr<ITimerTask> CreateTimer();

private:
	LRESULT WndProc(UINT nMsg, WPARAM wParam, LPARAM lParam);
	void RunTask();
	void OnTimer(UINT_PTR nTimerId);

	static CMainTaskThread *m_pThis;

	HWND    m_hWnd;
	WNDPROC m_pfnOldWndProc;
	bool    m_bHandling;
	UINT    m_nLastTimerId;
	std::unordered_map<UINT_PTR, std::shared_ptr<CUiTimerTask>> m_mapTimers;
};

class CUiTimerTask : public ITimerTask
{
public:
	CUiTimerTask(HWND hManager, UINT nTimerId) : m_hManager(hManager), m_nTimerId(nTimerId)
	{
	}

	void Start(UINT nElapse) override;
	void Stop() override;
	void Close() override;

private:
	HWND m_hManager;
	UINT m_nTimerId;
};

} // namespace AsyncLib
